package com.bc.web.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fs.common.extend.FSController;
import com.fs.common.util.FSJson;
import com.fs.common.util.FSJedis;
import com.bc.web.config.R;
import com.bc.web.model.Channel;
import com.bc.web.model.FeeModel;
import com.bc.web.model.Merch;
import com.bc.web.model.Org;
import com.bc.web.model.RiskRule;
import com.bc.web.model.TradeStrategy;

public class RedisController extends FSController {
	private static final int COMMIT_NUMBER = 1000;

	public void search() {
		render("basedata/redisList.html");
	}
	
	/**
	 * 加载渠道信息到Redis，存的是对象的JSON字符串
	 */
	public void channel() {
		Map<String, String> redisMap = new HashMap<String, String>();
		List<Channel> redisList = Channel.dao.selectToRedis();
		for (Channel c : redisList) {
			redisMap.put(c.getCode(), FSJson.toString(c));
		}
		FSJedis.set(R.RK_CHANNEL, redisMap);
		renderSuccess(false);
	}
	
	/**
	 * 加载机构信息到Redis，存的是字符串
	 */
	public void org() {
		Map<String, String> redisMap = new HashMap<String, String>();
		List<Org> redisList = Org.dao.selectToRedis();
		for (Org org : redisList) {
			redisMap.put(org.getCode(), org.getSafeKey());
		}
		FSJedis.set(R.RK_ORG, redisMap);
		renderSuccess(false);
	}


	/**
	 * 加载商户信息到Redis，存的是字符串
	 */
	public void merch() {
		Map<String, String> redisMap = new HashMap<String, String>();
		List<Merch> redisList = Merch.dao.selectToRedis();
		for (int i = 0; i < redisList.size(); i++) {
			redisMap.put(redisList.get(i).getCode(), FSJson.toString(redisList.get(i)));
			if ((i + 1) % COMMIT_NUMBER == 0) {
				FSJedis.set(R.RK_MERCH, redisMap);
				redisMap.clear();
			}
		}
		if (redisMap.size() > 0) {
			FSJedis.set(R.RK_MERCH, redisMap);
		}
		renderSuccess(false);
	}
	
	/**
	 * 加载交易策略到Redis，存的是字符串
	 */
	public void ts() {
		Map<String, String> redisMap = new HashMap<String, String>();
		List<TradeStrategy> redisList = TradeStrategy.dao.selectToRedis();
		for (int i = 0; i < redisList.size(); i++) {
			redisMap.put(redisList.get(i).getMerchCode() + redisList.get(i).getPayTypeCode(), FSJson.toString(redisList.get(i)));
			if ((i + 1) % COMMIT_NUMBER == 0) {
				FSJedis.set(R.RK_TS, redisMap);
				redisMap.clear();
			}
		}
		if (redisMap.size() > 0) {
			FSJedis.set(R.RK_TS, redisMap);
		}
		renderSuccess(false);
	}
	
	/**
	 * 加载风险规则到Redis，存的是字符串
	 */
	public void riskrule() {
		Map<String, String> redisMap = new HashMap<String, String>();
		List<RiskRule> redisList = RiskRule.dao.selectToRedis();
		for (int i = 0; i < redisList.size(); i++) {
			redisMap.put(redisList.get(i).getId().toString(), FSJson.toString(redisList.get(i)));
		}
		FSJedis.set(R.RK_RISK, redisMap);
		renderSuccess(false);
	}
	
	/**
	 * 加载费率模式到Redis，存的是对象的JSON字符串
	 */
	public void feemodel() {
		Map<String, String> redisMap = new HashMap<String, String>();
		List<FeeModel> redisList = FeeModel.dao.selectToRedis();
		for (int i = 0; i < redisList.size(); i++) {
			redisMap.put(redisList.get(i).getId().toString(), FSJson.toString(redisList.get(i)));
		}
		FSJedis.set(R.RK_FEE_MODEL, redisMap);
		renderSuccess(false);
	}
	
	/**
	 * 加载所有需要到Redis的数据
	 */
	public void all() {
		channel();
		org();
		merch();
		ts();
		riskrule();
		feemodel();
		renderSuccess(false);
	}

}
